<?php

include_once('Smarty.class.php');
$main_smarty = new Smarty;

include('config.php');
include(mnminclude.'html1.php');
include_once(mnminclude.'utils.php');
include(mnminclude.'ts.php');
include(mnminclude.'link.php');
include(mnminclude.'tags.php');
include(mnminclude.'group.php');
include(mnminclude.'search.php');
include(mnminclude.'smartyvariables.php');

// sidebar
$main_smarty = do_sidebar($main_smarty);

// pagename	
define('pagename', 'group'); 
$main_smarty->assign('pagename', pagename);

if (isset($_REQUEST['grp_id'])) {
	
	if (!isset($_REQUEST['doing'])) {
		$doing = 'viewrecopeople';
	}
	else {
		$doing = $_REQUEST['doing'];	
	}
	$main_smarty->assign('doing',$doing);
	
	switch ($doing) {
		case 'viewmembers':
			getgrpinfo();
			viewmembers();
			break;
		case 'viewrecopeople':
			getgrpinfo();
			viewrecopeople();
			break;
		case 'viewrecothing':
			getgrpinfo();
			viewrecothing();
			break;
		default:
			getgrpinfo();
			viewrecopeople();
			break;
	}
	
}

if (!isset($_REQUEST['grp_id'])) {
	//show all groups
	$doing = "viewgrouplist";
	$main_smarty->assign("doing",$doing);
	grouplist();
}

function getgrpinfo() {
	global $db, $dblang, $current_user, $main_smarty, $the_template;
	include_once('Smarty.class.php');
	include_once(mnminclude.'smartyvariables.php');
	$smarty = new Smarty;
	$grp_id = $_REQUEST['grp_id'];
	$group = new Group();
	$group->id = $grp_id;
	$group->read();
	$current_user_type = $group->get_user_type($current_user->user_id);
	$current_user_state = $group->get_user_state($current_user->user_id);
	$current_user_id = $current_user->user_id;
	$grp_pricacy = $group->privacy;
	$main_smarty->assign('current_user_type', $current_user_type);
	$main_smarty->assign('current_user_state', $current_user_state);
	$main_smarty->assign('current_user_id', $current_user_id);
	$main_smarty->assign('grp_privacy', $grp_pricacy);
	$grpinfo = $group->getgrpinfo();
	$main_smarty->assign('grpinfo', $grpinfo);
}

function viewmembers() {
	global $db, $dblang, $current_user, $main_smarty, $the_template, $rows, $offset;
	$top_users_size = 24;
	$usernames = '';
	if (isset($_POST['usernames'])) {
		$usernames = trim($_POST['usernames']);
	}
	$keystr = '';
	if ($usernames != '') {
		$keystr = " AND ( user_login LIKE '%".$_POST["usernames"]."%' OR user_names LIKE '%".$_POST["usernames"]."%' )";
	}
	$nouserhint = '';
	$grp_id = $_REQUEST['grp_id'];
	$group = new Group();
	$group->id = $grp_id;
	$group->read();
	$pusher = $group->getpusherview();
	$main_smarty->assign('pusher',$pusher);
	$current_user_type = $group->get_user_type($current_user->user_id);
	$main_smarty->assign('current_user_type',$current_user_type);
	
	$main_smarty->assign('cur_page',get_current_page());
	$offset=(get_current_page()-1)*$top_users_size;
	//$sqlquery = "SELECT COUNT(*) FROM " . table_group_user . " WHERE gu_group_id=" .$grp_id . " AND gu_user_state='approved'";
	$sqlquery = "SELECT COUNT(*) FROM " . table_users . "," . table_group_user . " where user_id=gu_user_id AND gu_user_state='approved' AND gu_group_id=" . $grp_id;
	$sqlquery .= $keystr;
	//die($sqlquery);
	$rows = $db->get_var($sqlquery);
	if (!($offset<$rows)) {
		$offset = 0;//in case the user change the keyword to search
		$main_smarty->assign('cur_page',1);
		$_POST['page'] = 1;//so the submit_user_do_page will not get wrong location
	}
	if ($rows>0) {
		$sqlquery = "SELECT * FROM " . table_users . "," . table_group_user . " where user_id=gu_user_id AND gu_user_state='approved' AND gu_group_id=" . $grp_id;
		$sqlquery .= $keystr;
		$sqlquery = $sqlquery . " LIMIT $offset,$top_users_size";
		$searchsql = mysql_query($sqlquery);
		$userlist = array();
				
		while ($row = mysql_fetch_array ($searchsql, MYSQL_ASSOC)) array_push ($userlist, $row);
		foreach($userlist as $key => $val){
			$userlist[$key]['Avatar'] = get_avatar('large', "", $val['user_login'], $val['user_email']);
			$userlist[$key]['User_Type_Ingrp'] = $group->get_user_type($val['user_id']);;
		}
		$main_smarty->assign('userlist', $userlist);
		$main_smarty->assign('rows', $rows);	
	}
	else {
		$nouserhint = '小组中无此用户';
	}
	$main_smarty->assign('nouserhint',$nouserhint);
	$main_smarty->assign('usernames', $usernames);
	$main_smarty->assign('grp_id', $grp_id);
	$main_smarty->assign('current_user_type',$current_user_type);
	$main_smarty->assign('tpl_center', $the_template . '/group_viewmembers');
	$main_smarty->display($the_template . '/pligg.tpl');
}

function viewrecopeople()  {
	global $db, $dblang, $current_user, $main_smarty, $the_template, $rows, $offset;
	$top_users_size = 24;
	$usernames = '';
	if (isset($_POST['usernames'])) {
		$usernames = trim($_POST['usernames']);
	}
	$keystr = '';
	if ($usernames != '') {
		$keystr = " AND ( user_login LIKE '%".$_POST["usernames"]."%' OR user_names LIKE '%".$_POST["usernames"]."%' )";
	}
	$nouserhint = '';
	$grp_id = $_REQUEST['grp_id'];
	$group = new Group();
	$group->id = $grp_id;
	$group->read();
	$pusher = $group->getrecopview_indexshow();
	$main_smarty->assign('pusher',$pusher);
	$current_user_type = $group->get_user_type($current_user->user_id);//die($current_user_type);
	$main_smarty->assign('current_user_type',$current_user_type);
	$main_smarty->assign('cur_page',get_current_page());
	$offset=(get_current_page()-1)*$top_users_size;
	$sqlquery = "SELECT COUNT(" . table_users . ".user_id) FROM " . table_users . ", " . table_link_owner . ", " . table_group_link . ", " . table_links . " WHERE " . table_users . ".user_id=" . table_link_owner . ".owner_id AND " . table_link_owner . ".link_id=" . table_group_link . ".gl_link_id AND " . table_group_link . ".gl_link_id=" . table_links . ".link_id AND " . table_links . ".link_category=" . get_category_id("人") . " AND " . table_group_link . ".gl_group_id=$grp_id AND " . table_links . ".link_status != 'discard'";
	$sqlquery .= $keystr;
	$rows = $db->get_var($sqlquery);
	if (!($offset<$rows)) {
		$offset = 0;//in case the user change the keyword to search
		$main_smarty->assign('cur_page',1);
		$_POST['page'] = 1;//so the submit_user_do_page will not get wrong location
	}
	if ($rows>0) {
		$sqlquery = "SELECT " . table_users . ".*, " . table_links . ".link_id " . " FROM " . table_users . ", " . table_link_owner . ", " . table_group_link . ", " . table_links . " WHERE " . table_users . ".user_id=" . table_link_owner . ".owner_id AND " . table_link_owner . ".link_id=" . table_group_link . ".gl_link_id AND " . table_group_link . ".gl_link_id=" . table_links . ".link_id AND " . table_links . ".link_category=" . get_category_id("人") . " AND " . table_group_link . ".gl_group_id=$grp_id AND " . table_links . ".link_status != 'discard'";
		$sqlquery .= $keystr;
		$sqlquery = $sqlquery . " LIMIT $offset,$top_users_size";
		$searchsql = mysql_query($sqlquery);
		$userlist = array();
				
		while ($row = mysql_fetch_array ($searchsql, MYSQL_ASSOC)) array_push ($userlist, $row);
		foreach($userlist as $key => $val){
			$userlist[$key]['Avatar'] = get_avatar('large', "", $val['user_login'], $val['user_email']);
			$userlist[$key]['User_Type_Ingrp'] = $group->get_user_type($val['user_id']);;
		}
		$main_smarty->assign('userlist', $userlist);
		$main_smarty->assign('rows', $rows);	
	}
	else {
		$nouserhint = '小组尚未推荐人';
	}
	$main_smarty->assign('nouserhint',$nouserhint);
	$main_smarty->assign('usernames', $usernames);
	$main_smarty->assign('grp_id', $grp_id);
	$main_smarty->assign('current_user_type',$current_user_type);
	$main_smarty->assign('tpl_center', $the_template . '/group_viewrecopeople');
	$main_smarty->display($the_template . '/pligg.tpl');
}

function viewrecothing()   {
	global $db, $dblang, $current_user, $main_smarty, $the_template, $rows, $offset;
	$top_users_size = 24;
	$usernames = '';
	if (isset($_POST['usernames'])) {
		$usernames = trim($_POST['usernames']);
	}
	$keystr = '';
	if ($usernames != '') {
		$keystr = " AND ( link_title LIKE '%".$_POST["usernames"]."%' )";
	}
	$nouserhint = '';
	$grp_id = $_REQUEST['grp_id'];
	$group = new Group();
	$group->id = $grp_id;
	$group->read();
	$pusher = $group->getrecolview_indexshow();
	$main_smarty->assign('pusher',$pusher);
	$current_user_type = $group->get_user_type($current_user->user_id);
	$main_smarty->assign('current_user_type',$current_user_type);
	$main_smarty->assign('cur_page',get_current_page());
	$offset=(get_current_page()-1)*$top_users_size;
	$sqlquery = "SELECT COUNT(" . table_links . ".link_id) FROM " . table_group_link . ", " . table_links . " WHERE "  . table_group_link . ".gl_link_id=" . table_links . ".link_id AND " . table_links . ".link_category=" . get_category_id("物") . " AND " . table_group_link . ".gl_group_id=$grp_id AND " . table_links . ".link_status != 'discard'";
	$sqlquery .= $keystr;
	$rows = $db->get_var($sqlquery);
	if (!($offset<$rows)) {
		$offset = 0;//in case the user change the keyword to search
		$main_smarty->assign('cur_page',1);
		$_POST['page'] = 1;//so the submit_user_do_page will not get wrong location
	}
	if ($rows>0) {
		$sqlquery = "SELECT " . table_links . ".* FROM " . table_group_link . ", " . table_links . " WHERE "  . table_group_link . ".gl_link_id=" . table_links . ".link_id AND " . table_links . ".link_category=" . get_category_id("物") . " AND " . table_group_link . ".gl_group_id=$grp_id AND " . table_links . ".link_status != 'discard'";
		$sqlquery .= $keystr;
		$sqlquery = $sqlquery . " LIMIT $offset,$top_users_size";
		$searchsql = mysql_query($sqlquery);
		$userlist = array();
				
		while ($row = mysql_fetch_array ($searchsql, MYSQL_ASSOC)) array_push ($userlist, $row);
		$main_smarty->assign('userlist', $userlist);
		$main_smarty->assign('rows', $rows);	
	}
	else {
		$nouserhint = '小组尚未推荐物';
	}
	$main_smarty->assign('nouserhint',$nouserhint);
	$main_smarty->assign('usernames', $usernames);
	$main_smarty->assign('grp_id', $grp_id);
	$main_smarty->assign('current_user_type',$current_user_type);
	$main_smarty->assign('tpl_center', $the_template . '/group_viewrecothing');
	$main_smarty->display($the_template . '/pligg.tpl');
}

function grouplist() {
	global $db, $dblang, $current_user, $main_smarty, $the_template, $rows, $offset;
	$top_users_size = 8;
	if ($current_user->authenticated) {
		//get mygroup
		$group = new Group();
		$mygroup = $group->getmygroup();
		$main_smarty->assign('mygroup', $mygroup);
	}
	$usernames = '';
	if (isset($_POST['usernames'])) {
		$usernames = trim($_POST['usernames']);
	}
	$keystr = '';
	if ($usernames != '') {
		$keystr = " AND ( link_title LIKE '%".$_POST["usernames"]."%' )";
	}
	$nouserhint = '';

	$main_smarty->assign('cur_page',get_current_page());
	$offset=(get_current_page()-1)*$top_users_size;
	$sqlquery = "SELECT COUNT(group_id) FROM " . table_group . "," . table_group_user . "," . table_users . " WHERE group_stage='normal' AND group_id=gu_group_id AND gu_user_id=user_id AND gu_user_type='owner'";
	$sqlquery .= $keystr;
	$rows = $db->get_var($sqlquery);
	if (!($offset<$rows)) {
		$offset = 0;//in case the user change the keyword to search
		$main_smarty->assign('cur_page',1);
		$_POST['page'] = 1;//so the submit_user_do_page will not get wrong location
	}
	if ($rows>0) {
		$sqlquery = "SELECT " . table_group . ".*, " . table_users . ".*" . " FROM " . table_group . "," . table_group_user . "," . table_users . " WHERE group_stage='normal' AND group_id=gu_group_id AND gu_user_id=user_id AND gu_user_type='owner'";
		$sqlquery .= $keystr;//die($sqlquery);
		$sqlquery = $sqlquery . " LIMIT $offset,$top_users_size";
		$searchsql = mysql_query($sqlquery);
		$userlist = array();
				
		while ($row = mysql_fetch_array ($searchsql, MYSQL_ASSOC)) array_push ($userlist, $row);
		$main_smarty->assign('userlist', $userlist);
		$main_smarty->assign('rows', $rows);	
	}
	else {
		$nouserhint = '不存在此小组';
	}
	$main_smarty->assign('nouserhint',$nouserhint);
	$main_smarty->assign('usernames', $usernames);
	$main_smarty->assign('tpl_center', $the_template . '/group_list');
	$main_smarty->display($the_template . '/pligg.tpl');
}

?>